//
//  QuotedSupervisorViewController.m
//  Supervisor
//
//  Created by 小宇 on 2017/5/12.
//  Copyright © 2017年 北京怀教网络科技股份有限公司. All rights reserved.
//

#import "QuotedSupervisorViewController.h"
#import "QuotedSVGCViewController.h"
#import "QuotedSVLCViewController.h"
#import "CommentTableViewCell.h"
#import "SelectViewController.h"
#import "MJRefreshHeader.h"
#import "QuotedSupervisorGC.h"

typedef NS_ENUM(NSUInteger, QuotedSupervisorState) {
    /** 未知网络*/
    QuotedSVGC,
    QuotedSVJL
};
@interface QuotedSupervisorViewController ()
@property (nonatomic,strong)UIView *segmentView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UILabel *labelGC;
@property (nonatomic,strong)UILabel *labelJL;
@property (nonatomic,strong)NSMutableArray * QuotedSV_GC_JL_List;
@property (nonatomic,strong)NSMutableDictionary *parameterDic;
@property (nonatomic,strong)NSString *url;
@property (nonatomic,assign)NSInteger pageNo;
@property (nonatomic,assign)QuotedSupervisorState state;
@end

@implementation QuotedSupervisorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.segmentView];
    self.url = procedurelist;
    [self loadDataRefresh];
    [self HeaderAndFooterRefresh];

}

- (NSString *)setNavTitle{
    return @"挂牌督导";
}

- (UIView *)segmentView{
    if (!_segmentView) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 45)];
        view.backgroundColor = [UIColor whiteColor];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(Main_Screen_Width / 2- 0.5, 15.5, 1, 20)];
        lineView.backgroundColor = LineColor;
        UIView *bootlineView = [[UIView alloc] initWithFrame:CGRectMake(0, 45, Main_Screen_Width, 5)];
        bootlineView.backgroundColor = BackgroundColor;
        [view addSubview:self.labelGC]; [view addSubview:lineView]; [view addSubview:self.labelJL]; [view addSubview:bootlineView];
        _segmentView = view;
    }
    return _segmentView;
}

- (UITableView *)set_tableViewWithFrame{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.segmentView.frame), Main_Screen_Width,Main_View_Height - CGRectGetMaxY(self.segmentView.frame))];
    self.tableView = tableView;
    return self.tableView;
}

- (void)HeaderAndFooterRefresh{
        LZYWeakSelf(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself loadDataRefresh];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakself loadMoreData];
    }];
}

- (void)loadDataRefresh{
    self.pageNo = 1;
    self.parameterDic = [[NSMutableDictionary alloc]initWithCapacity:3];
    [self.parameterDic setObject:[[User shareInstance] getUserAccessToken] forKey:@"accessToken"];
    [self.parameterDic setObject:@(self.pageNo) forKey:@"pageNo"];
    [self.parameterDic setObject:@(10) forKey:@"pageSize"];

    [LZYNetworkHelper GET:self.url parameters:self.parameterDic success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        self.QuotedSV_GC_JL_List = [NSMutableArray arrayWithArray:[QuotedSupervisorGC arrayOfModelsFromDictionaries:responseObject error:nil]];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    } showHub:YES];
}

- (void)loadMoreData{
    LZYWeakSelf(self)
    self.pageNo ++;
    [self.parameterDic setObject:[[User shareInstance] getUserAccessToken] forKey:@"accessToken"];
    [self.parameterDic setObject:@(self.pageNo) forKey:@"pageNo"];
    [self.parameterDic setObject:@(10) forKey:@"pageSize"];
    
    [LZYNetworkHelper GET:self.url parameters:self.parameterDic success:^(id responseObject) {
        if ([responseObject count] < 10 && [responseObject count] >0) {
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData];
            [weakself.QuotedSV_GC_JL_List addObjectsFromArray:[QuotedSupervisorGC arrayOfModelsFromDictionaries:responseObject error:nil]];
            [weakself.tableView reloadData];
        }else if([responseObject count] == 10){
            [weakself.tableView.mj_footer endRefreshing];
            [weakself.QuotedSV_GC_JL_List addObjectsFromArray:[QuotedSupervisorGC arrayOfModelsFromDictionaries:responseObject error:nil]];
            [weakself.tableView reloadData];
        }else if([responseObject count] == 0){
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSError *error) {
        
    }showHub:YES];
}


- (UILabel *)labelGC{
    if (!_labelGC) {
        _labelGC = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width / 2, 45) Text:@"督导过程" TextColor:MainColor TextAlignment:NSTextAlignmentCenter Font:AdaptedFontSize(13) Target:self Action:@selector(clickGC)];
    }
    return _labelGC;
}

- (UILabel *)labelJL{
    if (!_labelJL) {
        _labelJL = [[UILabel alloc] initWithFrame:CGRectMake(Main_Screen_Width /2 , 0, Main_Screen_Width / 2, 45) Text:@"督导记录" TextColor:MainFontColor TextAlignment:NSTextAlignmentCenter Font:AdaptedFontSize(13) Target:self Action:@selector(clickJL)];
    }
    return _labelJL;
}

- (UIButton *)set_rightButton{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 28, 20.5)];
    [button setTitle:@"新增" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = AdaptedFontSize(12);
    return button;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.tableView showBlankPageViewWithCount:self.QuotedSV_GC_JL_List reloadAction:@selector(loadDataRefresh) WithTarget:self];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentTableViewCell *cell = [CommentTableViewCell cellWithTableView:self.tableView];
    [cell settingWithModel:self.QuotedSV_GC_JL_List[indexPath.row]];
    cell.selectionStyle = UIAccessibilityTraitNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 83;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QuotedSupervisorGC * model = self.QuotedSV_GC_JL_List[indexPath.row];
    if (self.state == QuotedSVGC) {
        QuotedSVGCViewController * quotedSVGCVC = [[QuotedSVGCViewController alloc]init];
        quotedSVGCVC.quotedSVGCState = QuotedSVGCDetail;
        quotedSVGCVC.quotedSVId = [NSString stringWithFormat:@"%d",model.id];
        quotedSVGCVC.indexpath = indexPath;
        quotedSVGCVC.LzyReloadData = ^(NSIndexPath *reload) {
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        };
        [self.navigationController pushViewController:quotedSVGCVC animated:YES];
    }else{
        QuotedSVLCViewController * quotedSVGCLC = [[QuotedSVLCViewController alloc]init];
        quotedSVGCLC.quotedSVLCState = QuotedSVGCDetail;
        quotedSVGCLC.quotedSVId = [NSString stringWithFormat:@"%d",model.id];
        quotedSVGCLC.indexpath = indexPath;
        quotedSVGCLC.LzyReloadData = ^(NSIndexPath *reload) {
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        };
        [self.navigationController pushViewController:quotedSVGCLC animated:YES];
    }
}

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        //督导记录删除
        QuotedSupervisorGC *model = self.QuotedSV_GC_JL_List[indexPath.row];
        NSString *url;
        if (self.state == QuotedSVGC) {
            url = proceduredelete;
        }else{
            url = dudaoworkdelete;
        }
        [LZYNetworkHelper POST:url parameters:@{@"id":@(model.id)} success:^(id responseObject) {
            [LZYShowMsg showText:@"删除成功"];
            [self.QuotedSV_GC_JL_List removeObject:model];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        } failure:^(NSError *error) {
            [LZYShowMsg showText:@"删除失败"];
        } showHub:YES];
        }];
    return @[deleteRoWAction];
}


- (void)clickGC{
    self.labelGC.textColor = MainColor;
    self.labelJL.textColor = MainFontColor;
    self.state = QuotedSVGC;
    self.url = procedurelist;
    [self loadDataRefresh];
}

- (void)clickJL{
    self.labelGC.textColor = MainFontColor;
    self.labelJL.textColor = MainColor;
    self.state = QuotedSVJL;
    self.url = dudaoworklist;
    [self loadDataRefresh];
}

- (void)right_button_event:(UIButton *)sender{
    LZYWeakSelf(self)
    if (self.state == QuotedSVGC) {
        QuotedSVGCViewController * quotedSVGCVC = [[QuotedSVGCViewController alloc]init];
        quotedSVGCVC.quotedSVGCState = QuotedSVGCAdd;
        quotedSVGCVC.LzyReloadData = ^(NSIndexPath *indexpath) {
            [weakself.tableView.mj_header beginRefreshing];
        };
        [self.navigationController pushViewController:quotedSVGCVC animated:YES];
    }else{
        QuotedSVLCViewController * quotedSVLCVC = [[QuotedSVLCViewController alloc]init];
        quotedSVLCVC.quotedSVLCState = QuotedSVLCAdd;
        quotedSVLCVC.LzyReloadData = ^(NSIndexPath *indexpath) {
            [weakself.tableView.mj_header beginRefreshing];
        };
        [self.navigationController pushViewController:quotedSVLCVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
